package com.gzhryc.shared_device.oem.code;

import com.gzhryc.common.StringTools;
import com.gzhryc.common.exceptions.LogicException;
import com.gzhryc.common.json.JsonTools;
import com.gzhryc.common.logging.Logger;
import com.gzhryc.dictionary.services.ConfigInfoService;
import com.gzhryc.shared_device.bxscn.code.BXSCNCostConfig;
import com.gzhryc.shared_device.bxscn.code.outside.znst.OutsideZNSTConfig;
import com.gzhryc.shared_device.cdb.code.CDBCostConfig;
import com.gzhryc.shared_device.cdcz.code.CDCZCostConfig;
import com.gzhryc.shared_device.cdcz.code.dao.models.CDCZPowerIntervalMoneyContent;
import com.gzhryc.shared_device.oem.code.config.BusinessConfig;
import com.gzhryc.shared_device.oem.code.config.EConfigType;

import java.util.ArrayList;
import java.util.List;

public class ConfigUtils {

    static Logger log = Logger.getLogger(ConfigUtils.class);

    public static BusinessConfig getBusinessConfig(){
        BusinessConfig config = ConfigInfoService.self().getConfig(EConfigType.BusinessConfig.index(),BusinessConfig.class);
        if(config == null){
            config = new BusinessConfig();
        }
        return config;
    }

    public static boolean save(BusinessConfig config){
        return ConfigInfoService.self().save(EConfigType.BusinessConfig.index(),config);
    }

    public static CDCZCostConfig getCDCZCostConfig(){
        CDCZCostConfig config = ConfigInfoService.self().getConfig(EConfigType.CDCZCostConfig.index(),CDCZCostConfig.class);
        if(config == null){
            config = new CDCZCostConfig();
        }
        return config;
    }

    public static boolean save(CDCZCostConfig config){
        return ConfigInfoService.self().save(EConfigType.CDCZCostConfig.index(),config);
    }

    public static boolean addCDCZCostConfigPimContentItem(CDCZPowerIntervalMoneyContent.Item theItem) throws LogicException {
        CDCZCostConfig config = getCDCZCostConfig();
        CDCZPowerIntervalMoneyContent content = null;
        if(StringTools.isNotBlank(config.getPimContent())) {
            content = JsonTools.fromJson(config.getPimContent(), CDCZPowerIntervalMoneyContent.class);
        }else{
            content = new CDCZPowerIntervalMoneyContent();
        }
        if(content.getItems().size() > 0) {
            CDCZPowerIntervalMoneyContent.Item downItem = null;
            for (int i = 0; i < content.getItems().size(); i++) {
                CDCZPowerIntervalMoneyContent.Item item = content.getItems().get(i);
                if (item.getMaxPower().equals(theItem.getMinPower())) {
                    downItem = item;
                    break;
                }
            }
            if (downItem == null) {
                throw new LogicException("未找到关联的功率区间费用配置");
            }
        }

        content.getItems().add(theItem);
        config.setPimContent(JsonTools.toJson(content));
        return save(config);
    }

    public static boolean updateCDCZCostConfigPimContentItem(CDCZPowerIntervalMoneyContent.Item theItem) throws LogicException {
        CDCZCostConfig config = getCDCZCostConfig();
        if(StringTools.isNotBlank(config.getPimContent())) {
            CDCZPowerIntervalMoneyContent content = JsonTools.fromJson(config.getPimContent(), CDCZPowerIntervalMoneyContent.class);
            CDCZPowerIntervalMoneyContent.Item upItem = null;
            List<CDCZPowerIntervalMoneyContent.Item> items = new ArrayList<>();
            for (int i = 0; i < content.getItems().size(); i++) {
                CDCZPowerIntervalMoneyContent.Item item = content.getItems().get(i);
                if (item.getMinPower().equals(theItem.getMinPower())) {
                    if (!item.getMaxPower().equals(theItem.getMaxPower())) { //修改了最大功率
                        if ((i + 1) < content.getItems().size()) {
                            upItem = content.getItems().get((i + 1));
                            if (upItem.getMaxPower() < theItem.getMaxPower()) {
                                throw new LogicException("最大功率超过功率区间链");
                            } else {
                                upItem.setMinPower(theItem.getMaxPower());
                            }
                            i = i + 1;
                        }
                    }
                    item = theItem;
                }

                items.add(item);
                if (upItem != null) {
                    items.add(upItem);
                    upItem = null;
                }
            }

            content.setItems(items);
            config.setPimContent(JsonTools.toJson(content));
            return save(config);
        }
        return false;
    }

    public static boolean deleteCDCZCostConfigPimContentItem(Double minPower) throws LogicException {
        CDCZCostConfig config = getCDCZCostConfig();
        if(StringTools.isNotBlank(config.getPimContent())) {
            CDCZPowerIntervalMoneyContent content = JsonTools.fromJson(config.getPimContent(), CDCZPowerIntervalMoneyContent.class);
            CDCZPowerIntervalMoneyContent.Item theItem = null;
            CDCZPowerIntervalMoneyContent.Item upItem = null;
            CDCZPowerIntervalMoneyContent.Item downItem = null;
            List<CDCZPowerIntervalMoneyContent.Item> items = new ArrayList<>();
            for (int i = 0; i < content.getItems().size(); i++) {
                CDCZPowerIntervalMoneyContent.Item item = content.getItems().get(i);
                if (item.getMinPower().equals(minPower)) {
                    theItem = item;
                    if ((i + 1) < content.getItems().size()) {
                        upItem = content.getItems().get((i + 1));
                    } else if ((i - 1) >= 0) {
                        downItem = content.getItems().get((i - 1));
                    }
                    break;
                }
            }

            if (theItem == null) {
                throw new LogicException("不存在该功率区间");
            }

            if (upItem != null || downItem != null) {
                for (int i = 0; i < content.getItems().size(); i++) {
                    CDCZPowerIntervalMoneyContent.Item item = content.getItems().get(i);
                    if (!item.getMinPower().equals(minPower)) {
                        if (downItem != null && downItem.getMinPower().equals(item.getMinPower())) {
                            downItem.setMaxPower(minPower);
                            items.add(downItem);
                        } else if (upItem != null && upItem.getMinPower().equals(item.getMinPower())) {
                            upItem.setMinPower(minPower);
                            items.add(upItem);
                        } else {
                            items.add(item);
                        }
                    }
                }

                content.setItems(items);
                config.setPimContent(JsonTools.toJson(content));
                return save(config);
            }else{
                config.setPimContent(null);
                return save(config);
            }
        }
        return false;
    }

    public static CDBCostConfig getCDBCostConfig(){
        CDBCostConfig config = ConfigInfoService.self().getConfig(EConfigType.CDBCostConfig.index(),CDBCostConfig.class);
        if(config == null){
            config = new CDBCostConfig();
        }
        return config;
    }

    public static boolean save(CDBCostConfig config){
        return ConfigInfoService.self().save(EConfigType.CDBCostConfig.index(),config);
    }

    public static BXSCNCostConfig getBXSCNCostConfig(){
        BXSCNCostConfig config = ConfigInfoService.self().getConfig(EConfigType.BXSCNCostConfig.index(),BXSCNCostConfig.class);
        if(config == null){
            config = new BXSCNCostConfig();
        }
        return config;
    }

    public static boolean save(BXSCNCostConfig config){
        return ConfigInfoService.self().save(EConfigType.BXSCNCostConfig.index(),config);
    }

    public static OutsideZNSTConfig getOutsideZNSTConfig(){
        OutsideZNSTConfig config = ConfigInfoService.self().getConfig(EConfigType.OutsideZNSTConfig.index(), OutsideZNSTConfig.class);
        if(config == null){
            config = new OutsideZNSTConfig();
        }
        return config;
    }

    public static boolean save(OutsideZNSTConfig config){
        return ConfigInfoService.self().save(EConfigType.OutsideZNSTConfig.index(),config);
    }

}
